/**
 * 2013-6-26 上午8:06:18 Created By niexiaoqiang
 */

package com.xiaoqiang.MtwV2ForMoblie.update;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.xiaoqiang.MtwV2ForMoblie.MainActivity;
import com.xiaoqiang.MtwV2ForMoblie.R;

/**
 * 下载进度对话框
 */
public class DownloadDialog extends Dialog {

	public ProgressBar download_progressbar;
	private Button hiddenbuuton;
	private Button cancelButton;
	public Handler handle;
	public boolean isbuttonclose = false;

	public DownloadDialog(Context context, MainActivity mainActivity) {
		super(context, R.style.MyDialog);
		this.setContentView(R.layout.download_layout);
		handle = new DownloadDialogHandleExtension(mainActivity, this);
		download_progressbar = (ProgressBar) this.findViewById(R.id.download_progressbar);
		download_progressbar.setMax(100);
		hiddenbuuton = (Button) this.findViewById(R.id.download_hiddenbuuton);
		hiddenbuuton.setOnClickListener(new downloadDialogbuttonlistener(mainActivity));
		cancelButton = (Button) this.findViewById(R.id.download_cancelbuuton);
		cancelButton.setOnClickListener(new downloadDialogbuttonlistener(mainActivity));

	}

	/**
	 * @function 按钮事件监听器
	 * @author Administrator
	 * 
	 */
	class downloadDialogbuttonlistener implements android.view.View.OnClickListener {
		private MainActivity mainActivity;

		public downloadDialogbuttonlistener(MainActivity mainActivity) {
			this.mainActivity = mainActivity;
		}

		@Override
		public void onClick(View v) {
			if (((Button) v).getId() == R.id.download_hiddenbuuton) {
				isbuttonclose = true;
				hidden();
			} else {
				isbuttonclose = true;
				dismiss();
				mainActivity.downloadDialog = null;
				// 终止线程
				if (mainActivity.mtwDownloadApkFileThread.isAlive()) {
					mainActivity.mtwDownloadApkFileThread.shutdowndonwload();
				}
			}
		}
	}

	/*
	 * 判断是否是button点的
	 * @see android.app.Dialog#dismiss()
	 */
	public void dismiss() {
		if (isbuttonclose) {
			super.dismiss();
		}
	}

	/**
	 * 隐藏Dialog
	 */
	public void hidden() {
		super.hide();
	}

}
